package com.eva.ss.session;

import com.eva.framework.rbac.session.SessionAdapter;
import com.eva.ss.model.SsUserInfo;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

// v5.1.0，增加Bean名称，用于在Spring上下文中获取实例
@Component("ssSessionAdapter")
public class SsSessionAdapter implements SessionAdapter {

    @Override
    public SsUserInfo getLoginUser() {
        // v5.1.0，增加判断，当用户未登录时，principal为anonymousUser
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (principal == null) {
            return null;
        }
        if (principal instanceof String) {
            return null;
        }
        return (SsUserInfo) principal;
    }
}
